#include <QtTest>

#include <../JKProcessMonitor.h>
#include <QTextCodec>
#include <QRegularExpression>

class JKProcessMonitorTest : public QObject
{
    Q_OBJECT

public:
    JKProcessMonitorTest();
    ~JKProcessMonitorTest();

private slots:
    void initTestCase();
    void cleanupTestCase();
    void test_list();
    void test_connect();
    void test_show_dumpsys();
    void test_disconnect();

private:
    QString strCmd{"D:/GreenSoftware/scrcpy/adb.exe"};
};

JKProcessMonitorTest::JKProcessMonitorTest()
{

}

JKProcessMonitorTest::~JKProcessMonitorTest()
{

}

void JKProcessMonitorTest::initTestCase()
{
    QTextCodec::setCodecForLocale(QTextCodec::codecForName("GBK"));
}

void JKProcessMonitorTest::cleanupTestCase()
{

}

void JKProcessMonitorTest::test_list()
{
    QStringList arguments = QStringList() << "devices";
    JKProcessMonitor monitor;
    QByteArray baOutput;
    monitor.execute(strCmd, arguments, baOutput);

    QStringList lstDevice = QString(baOutput).split("\r\n");
    QRegularExpression reg("^(?<sn>\\S+)\\s+device$");
    foreach(QString line, lstDevice) {
        QRegularExpressionMatch matchResult = reg.match(line);
        if (matchResult.hasMatch()) {
            qDebug() << QStringLiteral("设备：") << matchResult.captured("sn");
        }
    }
    QString strContent = QString::fromLocal8Bit(baOutput);
    QString strListResult{"List of devices attached"};


    QVERIFY2(strContent.startsWith(strListResult), strContent.toLocal8Bit().constData());
}

void JKProcessMonitorTest::test_connect()
{
    QStringList arguments = QStringList() << "connect" << "192.168.2.72";
    JKProcessMonitor monitor;
    QByteArray baOutput;
    monitor.execute(strCmd, arguments, baOutput);
//    QFile f("d:/error.txt");
//    if (f.open(QIODevice::ReadWrite)) {
//        f.write(baOutput);
//        f.close();
//    }
    QString strContent = QString::fromUtf8(baOutput);
    QString strListResult{"connected to"};
    QVERIFY2(strContent.startsWith(strListResult), strContent.toLocal8Bit().constData());
}

void JKProcessMonitorTest::test_disconnect()
{
    QStringList arguments = QStringList() << "disconnect" << "192.168.2.72";
    JKProcessMonitor monitor;
    QByteArray baOutput;
    monitor.execute(strCmd, arguments, baOutput);
    QString strContent = QString::fromLocal8Bit(baOutput);
    QString strListResult{"disconnected"};
    QVERIFY2(strContent.startsWith(strListResult), strContent.toLocal8Bit().constData());
}

void JKProcessMonitorTest::test_show_dumpsys()
{
    QStringList arguments = QStringList() << "shell" << "dumpsys" << "window" << "policy";
    JKProcessMonitor monitor;
    QByteArray baOutput;
    monitor.execute(strCmd, arguments, baOutput);
    QRegularExpression reg("mAwake=(?<awake>\\w+)\\R(.*\\R)+\\s+mShowingLockscreen=(?<showingLockscreen>\\w+)");
    QRegularExpressionMatch matchResult = reg.match(baOutput);
    if (matchResult.hasMatch()) {
        qDebug() << QString("mAwake=%1, mShowingLockscreen=%2")
                    .arg(matchResult.captured("awake"))
                    .arg(matchResult.captured("showingLockscreen"));
    }

    QString strListResult{"WINDOW MANAGER POLICY STATE"};
    QString strContent = QString::fromLocal8Bit(baOutput);
    QVERIFY2(strContent.startsWith(strListResult), strContent.toLocal8Bit().constData());
}

QTEST_APPLESS_MAIN(JKProcessMonitorTest)

#include "tst_jkprocessmonitortest.moc"
